﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.IO;
using System.Drawing;

namespace MKDataBase.Contracts
{
    /// <summary>
    /// MovieDetailsDataRow
    /// </summary>
    public class MovieDetailsDataRow
    {
        /// <summary>
        /// Gets or sets the ID.
        /// </summary>
        /// <value>
        /// The ID.
        /// </value>
        public int ID { get; set; }

        /// <summary>
        /// Gets or sets the T itle.
        /// </summary>
        /// <value>
        /// The T itle.
        /// </value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the language.
        /// </summary>
        /// <value>
        /// The language.
        /// </value>
        public string Language { get; set; }

        /// <summary>
        /// Gets or sets the IMDB id.
        /// </summary>
        /// <value>
        /// The IMDB id.
        /// </value>
        public string IMDBId { get; set; }

        /// <summary>
        /// Gets or sets the IMDB URL.
        /// </summary>
        /// <value>
        /// The IMDB URL.
        /// </value>
        public string IMDBUrl { get; set; }

        /// <summary>
        /// Gets or sets the genre.
        /// </summary>
        /// <value>
        /// The genre.
        /// </value>
        public string Genre { get; set; }

        /// <summary>
        /// Gets or sets the releasedate.
        /// </summary>
        /// <value>
        /// The releasedate.
        /// </value>
        public DateTime Releasedate { get; set; }

        /// <summary>
        /// Gets or sets the rating.
        /// </summary>
        /// <value>
        /// The rating.
        /// </value>
        public float Rating { get; set; }

        /// <summary>
        /// Gets or sets the tag line.
        /// </summary>
        /// <value>
        /// The tag line.
        /// </value>
        public string TagLine { get; set; }

        /// <summary>
        /// Gets or sets the story line.
        /// </summary>
        /// <value>
        /// The story line.
        /// </value>
        public string StoryLine { get; set; }

        /// <summary>
        /// Gets or sets the image.
        /// </summary>
        /// <value>
        /// The image.
        /// </value>
        public byte[] MovieImage { get; set; }

        /// <summary>
        /// Maps to SqlParameterCollection
        /// </summary>
        /// <returns></returns>
        public IList<SqlParameter> MapTo()
        {
            List<SqlParameter> paramtrs = new List<SqlParameter>();
            paramtrs.Add(new SqlParameter(@"MovieID", this.ID));
            paramtrs.Add(new SqlParameter(@"Title", this.Title));
            paramtrs.Add(new SqlParameter(@"IMDBId", this.IMDBId));
            paramtrs.Add(new SqlParameter(@"IMDBUrl", this.IMDBUrl));
            paramtrs.Add(new SqlParameter(@"Genre", this.Genre));
            paramtrs.Add(new SqlParameter(@"ReleaseDate", this.Releasedate));
            paramtrs.Add(new SqlParameter(@"Rating", this.Rating));
            paramtrs.Add(new SqlParameter(@"TagLine", this.TagLine));
            paramtrs.Add(new SqlParameter(@"StoryLine", this.StoryLine));
            paramtrs.Add(new SqlParameter(@"Image", this.MovieImage));
            return paramtrs;
        }

        public XElement MapToXml()
        {
            string imgFilePath;
            try
            {
                imgFilePath = Directory.GetCurrentDirectory() + @"\\Images\img_" + this.Title + ".jpg";
                Bitmap img = new Bitmap(Image.FromStream(new MemoryStream(this.MovieImage)), new Size(180, 220));
                Bitmap map = new Bitmap(img);
                img.Dispose();
                map.Save(imgFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch {
                imgFilePath = string.Empty;
            }
            return new XElement("Movie", new XAttribute("Id", this.ID),
                new XElement("Title", this.Title),
                new XElement("IMDBId", this.IMDBId),
                new XElement("IMDBUrl", this.IMDBUrl),
                new XElement("Genre", this.Genre),
                new XElement("ReleaseDate", this.Releasedate),
                new XElement("Rating", this.Rating),
                new XElement("TagLine", this.TagLine),
                new XElement("StoryLine", this.StoryLine),
                new XElement("ImagePath", imgFilePath));
        }
    }
}
